﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.ETL.DSImplement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.ETL.TestApp
{
    class TestWSUtil
    {
        public class A
        {
            public string Id;
            public string Name;
            public B[] B;
        }
        public class B
        {
            public string B1;
            public string B2;
            public BI BI;
        }

        public class BI
        {
            public string BIID;
        }

        public class C
        {
            public string C1;
            public B[] B;
        }
        public class D
        {
            public string F1;
            public B B;
        }
        public class E
        {
            public string F1;
            public E[] E2;
        }
        public class A2
        {
            public string Id;
            public string Name;
            public B[] B;
            public C[] C;
        }
        public class A3
        {
            public string Id;
            public string Name;
            public C C;
        }
        public class A4
        {
            public string Id;
            public string Name;
            public C C1;
            public C C2;
        }
        public class A5
        {
            public string Id;
            public string Name;
            public C C1;
            public B[] B;
        }

        public class T1
        {
            public string Id;
            public string Name;
            public B B;
            public D D;
        }
        public class T2
        {
            public string Id;
            public string Name;
            public T2[] Items;
        }
        public static void Run()
        {
            string rootPath;
            Type elementType;
            if(WSTypeUtil.IsArrayObjectResult("RESULT", typeof(A), out rootPath, out elementType))
            {
                Console.WriteLine("true");
            }
            else
            {
                Console.WriteLine("false");
            }

            if (WSTypeUtil.IsArrayObjectResult("RESULT", typeof(A2), out rootPath, out elementType))
            {
                Console.WriteLine("true");
            }
            else
            {
                Console.WriteLine("false");
            }

            if (WSTypeUtil.IsArrayObjectResult("RESULT", typeof(A3), out rootPath, out elementType))
            {
                Console.WriteLine("true");
            }
            else
            {
                Console.WriteLine("false");
            }
            if (WSTypeUtil.IsArrayObjectResult("RESULT", typeof(A4), out rootPath, out elementType))
            {
                Console.WriteLine("true");
            }
            else
            {
                Console.WriteLine("false");
            }
            if (WSTypeUtil.IsArrayObjectResult("RESULT", typeof(A5), out rootPath, out elementType))
            {
                Console.WriteLine("true");
            }
            else
            {
                Console.WriteLine("false");
            }
        }

        public static void TestIsSupportResultType()
        {
            Type[] types = new Type[]{
                typeof(A),
                typeof(A2),
                typeof(A3),
                typeof(A4),
                typeof(A5),
                typeof(T1),
                typeof(T2)
            };

            foreach (var type in types)
            {
                if (WSTypeUtil.IsSupportResultType(type))
                {
                    Console.WriteLine(type.FullName + ":true");
                    string rootPath;
                    Type elementType;
                    if(WSTypeUtil.IsArrayObjectResult("RESULT", type, out rootPath, out elementType))
                    {
                        List<string> names = new List<string>();
                        List<Type> fTypes = new List<Type>();
                        List<Column> columns = CPPEI.ETL.DSImplement.WS.ArrayData.GetColumns(elementType, rootPath);
                        for (int index = 0; index < columns.Count; index++)
                        {
                            Console.WriteLine("c->" + columns[index].Name + ":" + columns[index].Path + ":" + columns[index].TypeFullName);
                        }
                    }
                    else if(WSTypeUtil.IsSingleObjectResult(type))
                    {
                        List<Column> columns = CPPEI.ETL.DataServiceInterface.ObjectData.GetColumns(type);
                        for (int index = 0; index < columns.Count; index++)
                        {
                            Console.WriteLine("c->" + columns[index].Name + ":" + columns[index].Path + ":" + columns[index].TypeFullName);
                        }
                    }
                }
                else
                {
                    Console.WriteLine(type.FullName + ":false");
                }
            }
        }
    }
}
